<?php
namespace app\admin\controller;

use app\common\controller\Adminbase;
use think\facade\Cache;
use app\admin\model\Procedure as ProcedureModel;
use app\admin\model\Bad as BadModel;
use app\admin\model\AdminUser;
use app\admin\model\Custom as CustomModel;
use think\Db;
class Procedure extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $where = [];
            $page = $this->request->param('page');
            $limit = $this->request->param('limit');
            if($this->request->param('filter')){
                $filter = $this->request->param('filter');
                $filter = json_decode($filter,true);
                foreach($filter as $k=>$row){
                    $where[] = [$k,'like','%'.trim($row).'%'];
                }
            }
            $offset = ($page - 1)*$limit;
            $result = ProcedureModel::where('deleted',0)->where($where)->limit($offset,$limit)->select();
            // dg($result);
            foreach($result as $k=>$row){
                $auth = explode(',',$row['auth']);
                $nickname = '';
                foreach($auth as $id){
                    if($id == 0){
                        $nickname .= '所有人';
                    }
                    $user = AdminUser::where('status',1)->find($id);
                    $nickname .= $user['nickname'].'，';
                }
                $nickname = rtrim($nickname,'，');
                $result[$k]['auth'] = $nickname;
                $bad = '';
                $bad_ids = explode(',',$row['bad_ids']);
                foreach($bad_ids as $id){
                    $badD = BadModel::where('deleted',0)->find($id);
                    $bad .= $badD['title'].'，';
                }
                $bad = rtrim($bad,'，');
                $result[$k]['bad'] = $bad;
                $data_lists = json_decode($row['data_list'],true);
                $data_list = '';
                if(is_array($data_lists)){
                    foreach($data_lists as $name){
                        $data_list.=$name.'，';
                    }
                }
                $data_list = rtrim($data_list,'，');
                $result[$k]['data_list'] = $data_list;
            }
            $count = ProcedureModel::where('deleted',0)->where($where)->count();
            $result = array("code" => 0, "count"=>$count,"data" => $result);
            return json($result);
        } else {
            return $this->fetch();
        }
    }

    public function add()
    {
        //报工权限
        $userList = AdminUser::where('status',1)->select();
        //不良品列表
        $badList = BadModel::where('deleted',0)->select();
        //自定义字段
        $customList = CustomModel::where(['type'=>'procedure','deleted'=>0])->select();
        $custom = [];
        foreach($customList as $k=>$row){
            $value = Db::name('gd_custom_'.$row['field_type'])->where('custom_id',$row['id'])->find();
            if($row['field_type'] == 'radio' || $row['field_type'] == 'checkbox'){
                $value['select'] = explode(',',$value['select']);
            }
            if($value['hidden']){
                $value['hidden'] = exchangeHidden($value['hidden'],'product');
            }
            $custom[] = [
                'title'=>$row['title'],
                'field_type'=>$row['field_type'],
                'attribute'=>$value
            ];
        }
        $admin = AdminUser::find(session('admin_user_auth')['uid']);
        $this->assign('custom',$custom);
        $this->assign('admin',$admin);
        $this->assign('userList',$userList);
        $this->assign('badList',$badList);
        return $this->fetch();
    }

    public function edit()
    {
        //报工权限
        $userList = AdminUser::where('status',1)->select();
        // dg($userList);
        //不良品列表
        $badList = BadModel::where('deleted',0)->select();

        $id = $this->request->param('id');
        $procedure = ProcedureModel::find($id);
        $procedure['auth'] = explode(',',$procedure['auth']);
        $procedure['bad_ids'] = explode(',',$procedure['bad_ids']);
        $procedure['data_list'] = json_decode($procedure['data_list'],true);
        // dg($procedure);
        //自定义字段
        $customList = CustomModel::where(['type'=>'procedure','deleted'=>0])->select();
        $custom = [];
        foreach($customList as $k=>$row){
            $value = Db::name('gd_custom_'.$row['field_type'])->where('custom_id',$row['id'])->find();
            if($row['field_type'] == 'radio' || $row['field_type'] == 'checkbox'){
                $value['select'] = explode(',',$value['select']);
            }
            if($value['hidden']){
                $value['hidden'] = exchangeHidden($value['hidden'],'product');
            }
            $custom[] = [
                'title'=>$row['title'],
                'field_type'=>$row['field_type'],
                'attribute'=>$value
            ];
        }
        //自定义字段值
        $values = Db::name('gd_extra_field')->where(['target_id'=>$id,'target'=>'product'])->select();
        // dg($values);
        foreach($custom as $k=>$row){
            if($values[$k]['name'] == 'extra_'.($k+1)){
                $custom[$k]['value'] = $values[$k]['value'];
            }
        }
        $admin = AdminUser::find(session('admin_user_auth')['uid']);
        $this->assign('custom',$custom);
        $this->assign('admin',$admin);
        $this->assign('procedure',$procedure);
        $this->assign('userList',$userList);
        $this->assign('badList',$badList);
        return $this->fetch();

    }

    public function editPost()
    {
        $data = $this->request->param();
        if(isset($data['name']) && $data['name'][0] != ''){
            $data_list = [];
            foreach($data['name'] as $k=>$name){
                $data_list[$data['type'][$k]] = $name;
            }
            $data['data_list'] = json_encode($data_list);
        }
        unset($data['name']);
        unset($data['type']);
        if(!isset($data['id'])){
            
            $check = ProcedureModel::where('nums',$data['nums'])->find();
            if(!empty($check)){
                $this->error('工序编号重复了');
            }

            $data['addtime'] = date('Y-m-d H:i:s');
            $insertData = [
                'data_list'=>$data['data_list'],
                'nums'=>$data['nums'],
                'title'=>$data['title'],
                'addtime'=>$data['addtime'],
                'auth'=>$data['auth'],
                'bad_ids'=>$data['bad_ids'],
                'ratio'=>$data['ratio'],
            ];
            ProcedureModel::insert($insertData);
        }else{
            unset($data['nums']);
            $data['mdtime'] = date('Y-m-d H:i:s');
            ProcedureModel::where('id',$data['id'])->update($data);
        }
        $this->success('添加成功');
    }

    public function delete()
    {
        $data = $this->request->param();
        ProcedureModel::where('id',$data['id'])->update(['deleted'=>1]);
        $this->success('删除成功',url('index'));
    }

    
}